package com.tedu.springboot2302.controller;


import com.tedu.springboot2302.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    private static File userDir;
    static {
        userDir=new File("./users");
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }
    @RequestMapping("/regUser")
//    HttpServletRequest称为请求对象,内容表达的是浏览器发送过来的所有数据
//    HttpServletResponse称为响应对象,内容表达的是我们处理完业务后给浏览器回复的内容
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理！！！");

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String ageStr=request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);

        if (username==null||username.isEmpty()||
            password==null||password.isEmpty()||
            nickname==null||nickname.isEmpty()||
            ageStr==null||ageStr.isEmpty()||
                !ageStr.matches("[0-9]+")
        ){
            //用于相应提出一个错误页面
            try {
                response.sendRedirect("/reg_info_error.html");//重定向
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        //到这里说明通过了验证
        int age=Integer.parseInt(ageStr);
        User user=new User(username,password,nickname,age);
        File file=new File(userDir,username+".obj");

        if (file.exists()){
            try {
                response.sendRedirect("/have_user");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }


        try(
                FileOutputStream fos=new FileOutputStream(file);
                ObjectOutputStream oos=new ObjectOutputStream(fos);
                ) {
            oos.writeObject(user);
            response.sendRedirect("/reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request,HttpServletResponse response){
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if (username==null||username.isEmpty()||
            password==null||password.isEmpty()){
            try {
                response.sendRedirect("/login_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        File file=new File(userDir,username+".obj");
        if (file.exists()){

            try (FileInputStream fis=new FileInputStream(file);
                 ObjectInputStream ois=new ObjectInputStream(fis);
            ){
                User user =(User) ois.readObject();
                if (user.getPassword().equals(password)){
                    response.sendRedirect("/login_success.html");
                }else {
                    response.sendRedirect("/login_fail.html");
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }


        }else {
            try {
                response.sendRedirect("/login_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @RequestMapping("/userList")
    public void userList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理显示用户列表");
        List<User> userList=new ArrayList<>();
        File[] subs=userDir.listFiles(f->f.getName().endsWith(".obj"));
        for (File sub:subs){
            try (
                    FileInputStream fis=new FileInputStream(sub);
                    ObjectInputStream ois=new ObjectInputStream(fis);
                    ){
               User user =(User) ois.readObject();
               userList.add(user);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        try {
//          在获取输出流前设置字符集
            response.setContentType("text/html;charset=utf-8");

//          通过响应对象获取一个输出流,使用该流写出的内容会发送给浏览器
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");

            pw.println("<table border='1'>");

            pw.println("<tr>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println("</tr>");

            for(User user : userList) {
                pw.println("<tr>");
                pw.println("<td>" + user.getUsername()  + "</td>");
                pw.println("<td>" + user.getPassword()  + "</td>");
                pw.println("<td>" + user.getNickname()  + "</td>");
                pw.println("<td>" + user.getAge()  + "</td>");
                pw.println("</tr>");
            }

            pw.println("</table>");

            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
